﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DLLPC.Utilities;
using PCBase.UIControls;

namespace COER
{
    public partial class UC_DataBlockForRTC : UserControl
    {
        public UC_DataBlockForRTC()
        {
            InitializeComponent();
            dlv.DoubleBufferedListView(true);
        }

        #region 新增控件标题&颜色
        [DefaultValue(typeof(string), "")]
        [Category("UCDataBlockView")]
        public string SetTitle
        {
            set
            {
                lblTitle.Text = value;
            }
            get
            {
                return lblTitle.Text;
            }
        }

        [DefaultValue(typeof(Color), "Blue")]

        [Category("UCDataBlockView")]
        public Color TitleColor
        {
            set { lblTitle.ForeColor = value; }
            get { return lblTitle.ForeColor; }
        }
        #endregion

        private List<LoadFileHelper.DataItem> dataItems = new List<LoadFileHelper.DataItem>();
        public void Manual_Load(string IOMapFile, string SheetName)
        {
            lvSymbolAddHead(dlv);
            dataItems = LoadFileHelper.LoadExcelFile(IOMapFile, SheetName);
            lvSymbolAddItem(dlv, dataItems);
        }

        public void RefreshListView(ushort[] dataBuff)
        {
            for (int i = 0; i < dataItems.Count;)
            {
                LoadFileHelper.DataItem varItem = dataItems[i];

                switch (varItem.DataType)
                {
                    case "BIN":
                        string StrBin = Convert.ToString(dataBuff[i], 2).PadLeft(16, '0');
                        varItem.objVal = string.Format("{0}-{1}-{2}-{3}", StrBin.Substring(0, 4), StrBin.Substring(4, 4), StrBin.Substring(8, 4), StrBin.Substring(12, 4));
                        break;
                    case "INT16":
                        varItem.objVal = dataBuff[i].ToString();
                        break;
                    case "INT32":
                        varItem.objVal = ((dataBuff[i + 1] * 65536) + dataBuff[i]).ToString();
                        break;
                    case "DBL_3":
                        varItem.objVal = (((dataBuff[i + 1] * 65536) + dataBuff[i]) * 0.001).ToString("F3");
                        break;
                    case "DBL_4":
                        varItem.objVal = (((dataBuff[i + 1] * 65536) + dataBuff[i]) * 0.0001).ToString("F4");
                        break;
                    case "STR":
                        varItem.objVal = GetString(dataBuff, i, varItem.Length * 2);
                        break;
                }

                for (int k = 0; k < varItem.Length; k++)
                {
                    dataItems[i + k].RowVal = dataBuff[i + k];
                }

                dlv.Items[i].SubItems[4].Text = dataItems[i].RowVal.ToString("X4");
                dlv.Items[i].SubItems[5].Text = dataItems[i].objVal.ToString();

                i += varItem.Length;
            }
        }

        private void lvSymbolAddHead(ListView lv)
        {
            lv.Items.Clear();
            lv.Columns.Clear();
            lv.GridLines = true;
            lv.View = View.Details;
            lv.FullRowSelect = true;
            lv.MultiSelect = false;
            lv.VirtualMode = false;

            lv.Columns.Add("ItemName", 60, HorizontalAlignment.Center);
            lv.Columns.Add("Comment", 220, HorizontalAlignment.Center);
            lv.Columns.Add("Type", 50);
            lv.Columns.Add("Len", 40);
            lv.Columns.Add("RawData", 60);
            lv.Columns.Add("Value", 150, HorizontalAlignment.Center);
        }

        private void lvSymbolAddItem(ListView lv, List<LoadFileHelper.DataItem> varItems)
        {
            int RowVal = 0x0000;
            object objVal = "";
            foreach (var item in varItems)
            {
                ListViewItem lvItem = new ListViewItem(item.Name);
                lvItem.SubItems.Add(item.Comment);
                lvItem.SubItems.Add(item.DataType);
                lvItem.SubItems.Add(item.Length.ToString());
                lvItem.SubItems.Add(item.RowVal.ToString("X4"));
                lvItem.SubItems.Add(item.objVal.ToString());
                lv.Items.Add(lvItem);
            }
        }

        private string GetString(ushort[] DataBuffer, int Index, int Length)
        {
            string strValue = "";
            byte[] byteTemp = new byte[Length];
            for (int i = 0; i < Length / 2; i++)
            {
                byte[] D1 = BitConverter.GetBytes(DataBuffer[Index + i]);
                byteTemp[2 * i] = D1[0];
                byteTemp[2 * i + 1] = D1[1];
            }
            for (int j = 0; j < Length; j++)
            {
                if (byteTemp[j] < 0x20)
                {
                    byteTemp[j] = 0x20;
                }
            }
            //strValue = BitConverter.ToString(byteTemp);
            strValue = System.Text.ASCIIEncoding.ASCII.GetString(byteTemp).Trim();
            return strValue.Trim();
        }
    }
}
